<?php

require('config.php');
$format = Annuaire::lookup(KEY_FORMAT);
//Vérifier que l'utilisateur est connecté
if(isset($_SESSION['id_user'])) {
	redirectFront('index');
}


$page = new rootLayoutCategory();
$page->setBaliseTitle('M\'inscrire sur Game');


//-- menu Left show products--------------------------------//
$menu = new layoutMenu();
$menuCategorie = new layoutMenuCategorie();
$menu->pushMenu($menuCategorie);
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenu($menu);

$tpl_file='inscription_validation.tpl';
$inscription = new flyLayout(REP_TPL . $tpl_file);
$inscription->start();

if(!isset($_POST['register'])){
redirectFront('index');
}
else {
	
    $birth_date = mktime(0, 0, 0, $_POST['birth_month'], $_POST['birth_day'], $_POST['birth_year']);    
        
	$birth_date = addslashes($birth_date);
	$pseudo = addslashes($_POST['pseudo']);
	$mot_de_passe = addslashes($_POST['mot_de_passe']);
	$your_email = addslashes($_POST['your_email']);
	$parent_email = addslashes($_POST['parent_email']);
	$language_default = $_POST['language_default'];
	$gender = $_POST['gender'];
	
$listEmail = array();
$your_email?array_push($listEmail, $your_email):'';
$parent_email?array_push($listEmail, $parent_email):'';

$user_registered = array();

	   if(sizeof($listEmail)>0){
		   for($i=1;$i<=sizeof($listEmail);$i++){
				$user = modelUser::getUserMail($listEmail[$i-1]);
				if(modelUser::PseudoExist($pseudo) == null){
						   $id_infos = modelUser::insertInfo($pseudo,$mot_de_passe,$birth_date,$language_default,$gender);
				}
				if($user){
					$id_user = $user->id_user;
					$info = modelUser::getUserInfo($id_user);

					if($id_infos && !modelUser::existIDIDUser($id_infos,$id_user)){
						modelUser::inscriptionInsertIDID($id_infos,$id_user);
					}else if($info->id_infos && !modelUser::existIDIDUser($info->id_infos,$id_user)){
						modelUser::inscriptionInsertIDID($info->id_infos,$id_user);
					}
					array_push($user_registered, $listEmail[$i-1]);
					$mail_inscription = $listEmail[$i-1];
					$user = modelUser::inscription($mail_inscription);
					$mail = new flyLayout(REP_MAIL.'inscription_more_account.mail.tpl');
					$mail->start();
					$mail->setVariable('pseudo', $info->pseudo);
					$mail->setVariable('language_default', modelLanguage::getLanguage($language_default)->name);
					$mail->setVariable('birth_date', $format->out($info->birth_date, Format::DATE));
					$mail->stop();
					$subject = 'New player account';
					
					if(STATUS_EMAIL == 0) {
		        		echo $mail;
		        	}elseif(STATUS_EMAIL == 1) {
		        		monsterMail($mail_inscription, $subject, $mail);
		        	}else {
		        		echo $mail;
		        		monsterMail($mail_inscription, $subject, $mail);
		        	}
					
					modelUser::inscriptionInsertIDID($id_infos,$user->id_user);
				}else{ // pas d'utilisateur enregistré avec cette adresse mail
				
					$mail_inscription = $listEmail[$i-1];
					$user = modelUser::inscription($mail_inscription);//echo HTTP_HOST.'validation.php?id='.$user->id_user.'&clef='.$user->key_unique;die();
					$mail = new flyLayout(REP_MAIL.'inscription.mail.tpl');
					$mail->start();
					$mail->setVariable('lien_activation', HTTP_HOST.'validation.php?id='.$user->id_user.'&clef='.$user->key_unique);
					$mail->setVariable('mail_utilisateur', $mail_inscription);
					$mail->stop();
					$subject = 'Validation de votre inscription sur '.MAIL_SITENAME;

					if(STATUS_EMAIL == 0) {
		        		echo $mail;
		        	}elseif(STATUS_EMAIL == 1) {
		        		monsterMail($mail_inscription, $subject, $mail);
		        	}else {
		        		echo $mail;
		        		monsterMail($mail_inscription, $subject, $mail);
		        	}

					modelUser::inscriptionInsertIDID($id_infos,$user->id_user);
					$new++;
				}
		   }
		   if($new>0){//at least 1 email inserted
				   //display page for back to home page
				   //echo 'at least 1 email inserted';exit();
				   $inscription->setVariable('http_host', HTTP_HOST);
				$inscription->showBlock('confirmation');
		   }
		   if($user_registered) {
				   for($i=1;$i<=sizeof($user_registered);$i++){
					   // l'utilisateur existe deja
					   $user = modelUser::getUserMail($user_registered[$i-1]);
					if($user->etat_user == modelUser::EMAIL_VALIDE) {
						// inscrit et adresse mail validée, on propose de récupérer le pass
						$inscription->setVariable('mail', $user_registered[$i-1]);
						$inscription->setVariable('http_host', HTTP_HOST);
						$inscription->parseList('list_oubli');
						$inscription->showBlock('oubli');
					}
					else {
						// inscrit mais adresse email non validée, on propose de renvoyé le mail
						$inscription->setVariable('mail', $user_registered[$i-1]);
						$inscription->parseList('list_activation');
						$inscription->showBlock('activation');
					}
				   }
		  }
	   }

}
        	
$inscription->stop();

$page->setBody($inscription);
$page->display();

?>